# 编写自动化脚本完成商城项目登录->浏览商品->下单功能（使用显式等待）
'''
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://mall.lemonban.com:3344/')
# 登录
loc_login = (By.XPATH,'//a[text()="登录"]')
# 用户名输入框
loc_username = (By.XPATH,'//input[@placeholder="请输入手机号/用户名"]')
# 密码输入框
loc_password = (By.XPATH,'//input[@placeholder="请输入密码"]')
# 登录按钮
loc_login_but = (By.CLASS_NAME,'login-button')

# 等待登录按钮可以点击后 再点击
WebDriverWait(driver,8,0.5).until(EC.element_to_be_clickable(loc_login)).click()
WebDriverWait(driver,8,0.5).until(EC.visibility_of_element_located(loc_username)).send_keys("lemon_py")
WebDriverWait(driver,8,0.5).until(EC.visibility_of_element_located(loc_password)).send_keys("12345678")
WebDriverWait(driver,8,0.5).until(EC.element_to_be_clickable(loc_login_but)).click()

# StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
# 元素引用异常，因为当点击登录按钮之后，页面会重新加载刷新，此时原来元素与现在的页面失去了联系，重新再去找最新的元素;
# 解决方案： 加一个强制等待。
sleep(2)
# 点击商品列表 浏览商品
loc_pro_list = (By.LINK_TEXT,'商品列表')
loc_pro_name = (By.XPATH,'//div[contains(text(),"lucky跑鞋04")]')
loc_buy = (By.CLASS_NAME,'buy-now')
loc_submit_order = (By.LINK_TEXT,'提交订单')
# 点击商品列表
WebDriverWait(driver,8,0.5).until(EC.element_to_be_clickable(loc_pro_list)).click()
# 点击商品名字
WebDriverWait(driver,8,0.5).until(EC.element_to_be_clickable(loc_pro_name)).click()
# 点击立即购买
WebDriverWait(driver,8,0.5).until(EC.element_to_be_clickable(loc_buy)).click()
# 点击提交订单
WebDriverWait(driver,8,0.5).until(EC.element_to_be_clickable(loc_submit_order)).click()

sleep(3)
driver.quit()


# 练习
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ES
import time

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://mall.lemonban.com:3344/")

local_login = (By.XPATH, "//a[text()='登录']")
local_username = (By.XPATH, "//input[@placeholder='请输入手机号/用户名']")
local_password = (By.XPATH, "//input[@placeholder='请输入密码']")
login_button = (By.XPATH, "//a[@class='login-button']")

WebDriverWait(driver, 8, 0.5).until(ES.element_to_be_clickable(local_login)).click()
WebDriverWait(driver, 8, 0.5).until(ES.visibility_of_element_located(local_username)).send_keys("root123")
WebDriverWait(driver, 8, 0.5).until(ES.visibility_of_element_located(local_password)).send_keys("root123")
WebDriverWait(driver, 8, 0.5).until(ES.element_to_be_clickable(login_button)).click()
time.sleep(2)

shoes_list = (By.XPATH,"//a[text()='商品列表']")
sort = (By.XPATH,"//span[contains(text(),'销量')]")
time.sleep(3)
shoes_name = (By.XPATH,"//div[contains(text(),'猫咪袜子')]")
shoes_buy = (By.XPATH,"//a[contains(text(),'立即购买')]")
shoes_send = (By.XPATH,"//a[contains(text(),'提交订单')]")
WebDriverWait(driver,8,0.5).until(ES.element_to_be_clickable(shoes_list)).click()
WebDriverWait(driver,8,0.5).until(ES.element_to_be_clickable(sort)).click()
WebDriverWait(driver,8,0.5).until(ES.element_to_be_clickable(shoes_name)).click()
WebDriverWait(driver,8,0.5).until(ES.element_to_be_clickable(shoes_buy)).click()
WebDriverWait(driver,8,0.5).until(ES.element_to_be_clickable(shoes_send)).click()
time.sleep(2)
'''

# 封装
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ES
import time

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://mall.lemonban.com:3344/")

def wait_element_clickable(locator):
    web_ment = WebDriverWait(driver,8,0.5).until(ES.element_to_be_clickable(locator))
    return web_ment

def wait_visibility_located(locator):
    web_ment = WebDriverWait(driver,8,0.5).until(ES.visibility_of_element_located(locator))
    return web_ment

local_login = (By.XPATH, "//a[text()='登录']")
local_username = (By.XPATH, "//input[@placeholder='请输入手机号/用户名']")
local_password = (By.XPATH, "//input[@placeholder='请输入密码']")
login_button = (By.XPATH, "//a[@class='login-button']")

wait_element_clickable(local_login).click()
wait_visibility_located(local_username).send_keys("root123")
wait_visibility_located(local_password).send_keys("root123")
wait_element_clickable(login_button).click()
time.sleep(2)

time.sleep(2)

shoes_list = (By.XPATH,"//a[text()='商品列表']")
sort = (By.XPATH,"//span[contains(text(),'销量')]")
shoes_name = (By.XPATH,"//div[contains(text(),'猫咪袜子')]")
shoes_buy = (By.XPATH,"//a[contains(text(),'立即购买')]")
shoes_send = (By.XPATH,"//a[contains(text(),'提交订单')]")
shoes_center = (By.XPATH,"//span[contains(text(),'个人中心')]")

wait_element_clickable(shoes_list).click()
wait_element_clickable(sort).click()
wait_element_clickable(shoes_name).click()
wait_element_clickable(shoes_buy).click()
wait_element_clickable(shoes_send).click()
time.sleep(3)
wait_element_clickable(shoes_center).click()
time.sleep(3)